(function(){
    angular.module('app').controller("agAudiencesCtrl",agAudiencesCtrl);

    function agAudiencesCtrl($scope,proxy){
        var vm = $scope;
        vm.obj={
            'permission':PERMISSION
        };
      
        //app下的监测-----显示
        vm.showSub = showSub; 
        vm.arrive = {};
        vm.conversion = {};
        vm.addArrive = addArrive; 
        vm.showMask = showMask; 
        vm.addConversion = addConversion; 
        vm.setStatus = setStatus;
        vm.showCode = showCode;
        vm.showInfo = showInfo;
        vm.closeLayer = function () {layer.closeAll();};

        getList();

        function getList(current, pagesize) {
            var data = {
                'page': (current || 1)+"",
                'size': (pagesize || 20)+"",
                "sort": ["cTime,1"],
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/monitor/list',
                data: data,
                load:true,
                success: function(res){
                    // 获得列表
                    vm.list = res.data.data;
                    $("#pageToolbar").html("");
                    $('#pageToolbar').Paging({pagesize: pagesize || '20',current: current || '1',count:res.data.total,toolbar:true,'hash': false,
                        callback:getList});
                }
            })
        };
    
        function showSub(item){
            item.flag = !item.flag;
        };

        function addArrive() {
            var data = vm.arrive;
            if(!data.name) return layer.msg("请输入监测站点名称！")
            if(!data.url) return layer.msg("请输入监测站点url！")
            add(data);
        }
        function addConversion() {
            var data = vm.conversion;
            if(!data.name) return layer.msg("请输入监测站点名称！")
            add(data);
        }

        function add(data) {
            proxy.proxy({
                url: STATIC_URL.domian + '/dsp/monitor/add',
                data: {
                    data: data
                },
                load: true,
                success: function(){
                    layer.msg("操作成功");
                    getList();
                    $("#arriveModal,#conversionModal").modal("hide");
                }
            })
        }

        function showMask(type,item) {
            if(type == 1) {
                vm.arrive = {};
                $("#arriveModal").modal();
            } else {
                vm.conversion = {
                    parentID: item.id
                }
                $("#conversionModal").modal();
            }
        }
        function showInfo() {
            layer.open({
                type: 1,
                title: "落地页宏替换",
                content: $('.lp-info-box'),
                area:["672px","534px"]
            })
        }

        function showCode(item,type) {
            if(type != "arrive") {
                vm.code = '<!-- 说明：这一行代码放在页面上 -->\n<script src="http://res1.hubcloud.com.cn/sdk/monitor.js"></script>\n\n<!-- 说明：这一行代码放在行为函数上 -->\nadhub_monitor({src: "'+item.code+'",action: "'+type+'"})';
            } else {
                vm.code = '<script src="http://res1.hubcloud.com.cn/sdk/monitor.js"></script>\n<script>adhub_monitor({src: "'+item.code+'",action: "'+type+'"})</script>';
            }
            layer.open({
                type: 1,
                title: "html代码",
                content: $('.copy-box'),
                area:["670px","410px"]
            })
        }

        function setStatus(item) {
            proxy.proxy({
                url: STATIC_URL.domian + '/dsp/monitor/modify/state',
                data: {
                    data: {
                        ids: [item.id],
                        state: item.open?"10":"20"
                    }
                },
                load: true,
                success: function(){
                    layer.msg("操作成功");
                },
                error: function() {
                    item.open = !item.open;
                }
            })
        }
      
    }
})();